package org.abstractmeta.commandify.core.command;

import org.abstractmeta.commandify.OwnerSelfCommandifyAccessor;
import org.abstractmeta.commandify.command.Command;

/**
* Created with IntelliJ IDEA.
* User: adrianwit
* Date: 6/28/13
* Time: 7:51 PM
* To change this template use File | Settings | File Templates.
*/
public class AncestorCommand implements Command<Object> {

    private final OwnerSelfCommandifyAccessor ancestorPath;
    private final Command command;

    public AncestorCommand(OwnerSelfCommandifyAccessor ancestorAccessor, Command command) {
        this.ancestorPath = ancestorAccessor;
        this.command = command;
    }

    @Override
    public String getSource() {
        return "" + ancestorPath.getSource();
    }

    @Override
    @SuppressWarnings("unchecked")
    public void execute(Object instance) {
        Object value = ancestorPath.getAccessor().get(instance);
        if (value != null) {
            command.execute(value);
        }
    }

    public OwnerSelfCommandifyAccessor getAncestorPath() {
        return ancestorPath;
    }

    public Command getCommand() {
        return command;
    }
}
